package com.project.slsx;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.project.slsx.activity.BaseActivity;
import com.project.slsx.fragment.DatasFragment;
import com.project.slsx.fragment.GoodsFragment;
import com.project.slsx.fragment.MineFragment;
import com.project.slsx.fragment.OrderFragment;

import java.util.ArrayList;

public class MainActivity extends BaseActivity implements View.OnClickListener {

    private FrameLayout container;
    private RelativeLayout data;
    private RelativeLayout order;
    private RelativeLayout goods;
    private RelativeLayout mine;
    private ArrayList<Fragment> fragments;
    public static final int ZERO = 0;
    public static final int ONE = 1;
    public static final int TWO = 2;
    public static final int THREE = 3;
    private static final int FOUTH = 4;
    private int currentTab = -1;


    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        container = findViewById(R.id.fl_container);
        data = findViewById(R.id.rl1);
        order = findViewById(R.id.rl2);
        goods = findViewById(R.id.rl3);
        mine = findViewById(R.id.rl4);
        data.setOnClickListener(this);
        order.setOnClickListener(this);
        goods.setOnClickListener(this);
        mine.setOnClickListener(this);

        fragments = new ArrayList<>();
        fragments.add(new DatasFragment());
        fragments.add(new OrderFragment());
        fragments.add(new GoodsFragment());
        fragments.add(new MineFragment());
//        fragments.add(new MineLoginFragment());

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.fl_container, fragments.get(ZERO));
        currentTab = ZERO;
        data.setSelected(true);
        ft.commit();
    }

    @Override
    public void initData() {

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.rl1:
                changeTab(ZERO);
                break;
            case R.id.rl2:
                changeTab(ONE);
                break;
            case R.id.rl3:
                changeTab(TWO);
                break;
            case R.id.rl4:
                changeTab(THREE);
                break;
        }
    }

    public void changeTab(int index) {
        if (currentTab == index) {
            return;
        }
        Fragment fragment = fragments.get(index);
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        if (!fragment.isAdded()) {
            ft.add(R.id.fl_container, fragment);
        }

        ft.hide(fragments.get(currentTab));
        ft.show(fragments.get(index));
        changeTabStatus(currentTab, false);
        currentTab = index;
        changeTabStatus(currentTab, true);
        if (!this.isFinishing()) {
            ft.commitAllowingStateLoss();
        }
    }

    private void changeTabStatus(int currentTab, boolean isSelect) {
        switch (currentTab) {
            case ZERO:
                data.setSelected(isSelect);
                break;
            case ONE:
                order.setSelected(isSelect);
                break;
            case TWO:
                goods.setSelected(isSelect);
                break;
            case THREE:
                mine.setSelected(isSelect);
                break;
            case FOUTH:
                mine.setSelected(isSelect);
                break;

        }
    }
}
